﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using ImageResizer.Properties;
using Manina.Windows.Forms;

namespace ImageResizer
{
    public partial class ImageResizerDialog : Form
    {
        #region CTOR
        /// <summary>
        /// CTOR
        /// </summary>
        public ImageResizerDialog()
        {
            InitializeComponent();
        } 
        #endregion

        #region Private vars
        /// <summary>
        /// Initial directory
        /// </summary>
        private DirectoryInfo _initialDirectory;

        /// <summary>
        /// Content of directory
        /// </summary>
        private FileInfo[] _directoryContent;
        #endregion
        

        /// <summary>
        /// Change initial directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonOpenFolderClick(object sender, EventArgs e)
        {
            _initialDirectory = null;
            folderBrowserDialog1.SelectedPath = Settings.Default.LastPhotoDir;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                _initialDirectory = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                Settings.Default.LastPhotoDir = folderBrowserDialog1.SelectedPath;
                Settings.Default.Save();

            }
            LoadDirectory();
        }

        /// <summary>
        /// Load initial directory : list files (not recursively)
        /// </summary>
        private void LoadDirectory()
        {
            if (_initialDirectory == null)
            {
            }
            else
            {
                 //get all images from directory
                _directoryContent = _initialDirectory.GetFiles("*.jpg");
                imageListView1.Items.Clear();
                imageListView1.Items.AddRange(_directoryContent.ToFileNames());
                
            }
        }

        /// <summary>
        /// Open the process dialog box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonProcessClick(object sender, EventArgs e)
        {
            if (!File.Exists(Settings.Default.AlbulleConfig))
            {
                MessageBox.Show(Resources.ImageResizerDialog_ButtonProcessClick_ + Settings.Default.AlbulleConfig, Resources.ImageResizerDialog_ButtonProcessClick_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (_initialDirectory == null || imageListView1.CheckedItems.Count()==0)
            {
                MessageBox.Show(Resources.ImageResizerDialog_ButtonProcessClick_Veuillez_choisir_des_images_à_redimensionner, Resources.ImageResizerDialog_ButtonProcessClick_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return ;
            }
          

            List<string> checkedItems = new List<string>();
            foreach(ImageListViewItem v in imageListView1.CheckedItems)
                checkedItems.Add(v.FileName);

            ProcessDialog processor = new ProcessDialog(checkedItems, _initialDirectory.Name);
            processor.ShowDialog();
        }



        private void ButtonBrowseClick(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Settings.Default.AlbulleConfig))
                openFileDialog1.FileName = "config.php";
            else
                openFileDialog1.FileName = Settings.Default.AlbulleConfig;
            openFileDialog1.Filter = Resources.ImageResizerDialog_ButtonBrowseClick_Php_file___php;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Settings.Default.AlbulleConfig = openFileDialog1.FileName;
                Settings.Default.Save();
                textBox_config.Text = Settings.Default.AlbulleConfig;
            }
        }

        private void ImageResizerDialog_Load(object sender, EventArgs e)
        {
            textBox_config.Text = Settings.Default.AlbulleConfig;
        }

        private void ButtonDonateClick(object sender, EventArgs e)
        {
            string url = "";

            string business = "decoretjm@gmail.com";  // your paypal email
            string description = "Donation";            // '%20' represents a space. remember HTML!
            string country = "FR";                  // AU, US, etc.
            string currency = "EUR";                 // AUD, USD, etc.

            url += "https://www.paypal.com/cgi-bin/webscr" +
                "?cmd=" + "_donations" +
                "&business=" + business +
                "&lc=" + country +
                "&item_name=" + description +
                "&currency_code=" + currency +
                "&bn=" + "PP%2dDonationsBF";
            System.Diagnostics.Process.Start(url);
        }

    }

    static class MyExtension
    {
        public static string[] ToFileNames(this FileInfo[] obj)
        {
            string[] res = new string[obj.Count()];

            for(int i=0; i<obj.Count(); i++)
                res[i] = obj[i].FullName;
            return res;
        }
    }
}

